<template>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="account" prop="account">
            <el-input v-model="ruleForm.account"></el-input>
        </el-form-item>
        <el-select v-model="value" placeholder="请选择">
            <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
            </el-option>
        </el-select>
        <el-form-item label="realname" prop="realname">
            <el-input v-model="ruleForm.realname"></el-input>
        </el-form-item>
        <el-form-item label="sex" prop="sex">
            <el-radio-group v-model="ruleForm.sex">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="birthday" prop="birthday">
            <el-col :span="11">
                <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.birthday" style="width: 100%;"></el-date-picker>
            </el-col>
        </el-form-item>
        <el-form-item label="phone" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm()">添加</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import {add} from "../../api/user";

    export default {
        name: "add-index",
        data() {
            return {
                ruleForm: {
                    account: '',
                    realname: '',
                    sex: '',
                    birthday: '',
                    phone: ''
                },
                rules: {
                    account: [
                        { required: true, message: '请输入活动名称', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    realname: [
                        { required: true, message: '请选择活动区域', trigger: 'change' }
                    ],
                    birthday: [
                        { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
                    ]
                }
            };
        },
        methods: {
            submitForm() {
                if(this.ruleForm.sex == "男"){
                    this.ruleForm.sex = 1;
                }else {
                    this.ruleForm.sex = 0;
                }
                add(this.ruleForm).then(result =>{
                    if(result==1){
                        this.$router.replace("/user/list")
                    }
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>